package com.example.server.utils;


import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.UUID;

public class JWTUtils {


    private static final String SECRET_KEY = "woniu123";

    public static String generateToken(String username) {
        return Jwts.builder()
                .setSubject(username+":"+UUID.randomUUID().toString().replace("-", ""))
                .signWith(SignatureAlgorithm.HS512, SECRET_KEY)
                .compact();
    }


    public static boolean validateToken(String token) {
        String username = Jwts.parser()
                .setSigningKey(SECRET_KEY)
                .parseClaimsJws(token)
                .getBody()
                .getSubject();
        System.out.println("username: " + username);
        return true;
    }

    public static void main(String[] args) {
//        String jwt = generateToken("zhangsan");
//        System.out.println(jwt);
        boolean b = validateToken("euJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ6aGFuZ3NhbjphNWFiYTk5NmE0MWE0NGFkODg5MGI0Nzg5NWM5ZmEwYiJ9.Dke06AQ57bqHqkgv8EO5SoChyznX6rlE4-PPiTfKQvfweuH-jMAeNXTU9XVQrhLPXtWNdG9RN9D0ugbzsUMQ8g");
        System.out.println(b);
    }
}
